package cn.nkjobsearch.convert;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Salary {
	public static int convert(String salary, char fromSite) {
		if(fromSite == '5') {
			return job51(salary);
		} else if(fromSite == 'c') {
			return chinaHR(salary);
		} else if(fromSite == 'z') {
			return zhilian(salary);
		} else {
			return 0;
		}
	}
	
	private static int job51(String salary) {
		if (salary == null || salary.equals("null") || salary.equals("面议")) {
			return 0;
		}
		try {
			int separationIndex = salary.indexOf((int) '-');
			if (separationIndex > 3) {
				return Integer.parseInt(salary.substring(0, separationIndex));
			} else {
				Pattern p = Pattern.compile("[0-9]{4,6}",
						Pattern.CASE_INSENSITIVE);
				Matcher m = p.matcher(salary);
				if (m.find()) {
					return Integer.parseInt(salary
							.substring(m.start(), m.end()));
				}
			}
		} catch (Exception e) {
			return 0;
		}
		return 0;
	}

	private static int zhilian(String salary) {
		if (salary == null || salary.equals("null") || salary.equals("面议")) {
			return 0;
		}
		try {
			int separationIndex = salary.indexOf((int) '-');
			if (separationIndex > 3) {
				return Integer.parseInt(salary.substring(0, separationIndex));
			} else {
				Pattern p = Pattern.compile("[0-9]{4,6}",
						Pattern.CASE_INSENSITIVE);
				Matcher m = p.matcher(salary);
				if (m.find()) {
					return Integer.parseInt(salary
							.substring(m.start(), m.end()));
				}
			}
		} catch (Exception e) {
			return 0;
		}
		return 0;
	}
	
	private static int chinaHR(String salary) {
		if (salary == null || salary.equals("null") || salary.equals("面议")) {
			return 0;
		}
		try {
			int separationIndex = salary.indexOf("～");
			if (separationIndex > 3) {
				return Integer.parseInt(salary.substring(0, separationIndex));
			} else {
				Pattern p = Pattern.compile("[0-9]{4,6}",
						Pattern.CASE_INSENSITIVE);
				Matcher m = p.matcher(salary);
				if (m.find()) {
					return Integer.parseInt(salary
							.substring(m.start(), m.end()));
				}
			}
		} catch (Exception e) {
			return 0;
		}
		return 0;
	}
}
